package com.demo.core.dao.impl;

import java.util.List;
import java.util.Map;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.SiteFunctionDao;
import com.demo.core.dao.impl.base.BaseCoreDaoSupport;
import com.demo.core.model.SiteFunction;
import com.demo.core.model.support.GeneralExecuteModel;
import com.demo.core.model.support.converter.AdjacencyListToChainedObjectConverter;

/**
 * 站点功能数据访问层实现类
 * 
 * @author ryuu.kk
  */
public class SiteFunctionDaoImpl extends BaseCoreDaoSupport<SiteFunction, Long> implements SiteFunctionDao {

	/**
	 * 站点空间 
	 */
	protected final static String SITE_FUNCTION_SPACE = "siteFunctionSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_SITE_FUNCTION";
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	protected String nameSpace() {
		return SITE_FUNCTION_SPACE;
	}
	
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
		model.setNameSpace(SITE_FUNCTION_SPACE);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SiteFunction> queryFunctionUnion(Map<String, Object> params) {
		CacheKey key = null;
		List<SiteFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update(nameSpace() + "querySiteFunction");
			key.update(params.hashCode());
			result = (List<SiteFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(nameSpace() + "querySiteFunction", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public SiteFunction queryFunctionTree(Map<String, Object> params) {
		
		CacheKey key = null;
		List<SiteFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update(nameSpace() + "queryFunctionTree");
			key.update(params.hashCode());
			result = (List<SiteFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  =  getSqlMapClientTemplate().queryForList(nameSpace() + "queryFunctionTree", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		
		return AdjacencyListToChainedObjectConverter.conver(result);
	}
	@SuppressWarnings("unchecked")
	@Override
	public SiteFunction querySiteFunctionTree(Map<String, Object> params) {
		CacheKey key = null;
		List<SiteFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update(nameSpace() + "querySiteFunctionTree");
			key.update(params.hashCode());
			result = (List<SiteFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(nameSpace() + "querySiteFunctionTree", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return AdjacencyListToChainedObjectConverter.conver(result);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<Long, SiteFunction> querySiteFunctionMap(
			Map<String, Object> params) {
		String name = "querySiteFunctionMap";
		
		CacheKey key = null;
		Map<Long, SiteFunction> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			// 增加一个条件,防止key的HashCode无法区分普通检索和联合查询.
			key.update(name);
			key.update(params.hashCode());
			result = (Map<Long, SiteFunction>) cache.get(key);
		}
		
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForMap(nameSpace() + name, params, "functionId");
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SiteFunction> querySiteFunctionAndRoleList(Map<String, Object> params) {
		return getSqlMapClientTemplate().queryForList(nameSpace() + "querySiteFunctionAndRoleMap", params);
	}
}
